/*
 * @Author: wangyifei 844650674@qq.com
 * @Date: 2022-11-27 10:22:27
 * @LastEditors: wangyifei 844650674@qq.com
 * @LastEditTime: 2022-11-27 13:45:55
 * @FilePath: \management_system_template\src\service.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
export function ajaxRequest(url, method, params) {
  return new Promise((resolve, reject) => {
    let data;
    if (method === "POST" || method === "PUT") {
      data = JSON.stringify(params);
    } else {
      data = null;
    }
    $.ajax({
      type: method,
      url,
      data,
      contentType: "application/json;charset=utf-8",
      beforeSend: function(request) {
        if (localStorage.getItem("userInfo")) {
          request.setRequestHeader(
            "Authorization",
            JSON.parse(localStorage.getItem("userInfo")).token
          );
        }
      },

      success: (res) => {
        resolve(res);
      },
      error: (jqXHR, textStatus, errorThrown) => {
        reject(textStatus);
      },
    });
  });
}

// 登录
export const login = (params) => {
  return ajaxRequest("/user/login", "POST", params);
};

// 注册
export const register = (params) => {
  return ajaxRequest("/user/register", "POST", params);
};

// 编辑用户
export const updateUser = (id, params) => {
  return ajaxRequest(`/user/update/${id}`, "POST", params);
};

// 新建用户
export const addUser = (params) => {
  return ajaxRequest("/users", "POST", params);
};

// 获取用户列表
export const getUser = (params) => {
  return ajaxRequest("/user/users", "GET", params);
};

// getUserInfo
export const getUserInfo = (id) => {
  return ajaxRequest(`/user/${id}`, "GET");
};

export const hasAdmin = (params) => {
  return ajaxRequest(`/users/hasAdmin`, "POST", params);
}

// 删除用户
export const deleteUser = (id) => {
  return ajaxRequest(`/user/${id}`, "DELETE");
}

// 获取模板信息
export const getModel = () => {
  return ajaxRequest(`/model`, "GET");
}

// 新建模板
export const addModel = (params) => {
  return ajaxRequest(`/model`, "POST", params);
};


export const deleteModel = (id) => {
  return ajaxRequest(`/model/${id}`, "DELETE");
}

// 编辑用户
export const updateModel = (id, params) => {
  return ajaxRequest(`/model/${id}`, "POST", params);
};


// 职位相关接口
export const addJob = (params) => {
  return ajaxRequest(`/job`, "POST", params);
};
export const getJob = (params) => {
  return ajaxRequest(`/job`, "GET", params);
};

export const deleteJob = (id) => {
  return ajaxRequest(`/job/${id}`, "DELETE");
};

export const updateJob = (id, params) => {
  return ajaxRequest(`/job/${id}`, "POST", params);
};
export const getJobSalary = (params) => {
  return ajaxRequest(`/jobSalary`, "GET", params);
};
export const getJobTags = (params) => {
  return ajaxRequest(`/jobTags`, "GET", params);
};
export const getJobExperience = (params) => {
  return ajaxRequest(`/jobExperience`, "GET", params);
};
export const getJobLocation = (params) => {
  return ajaxRequest(`/jobLocation`, "GET", params);
};



// 角色相关接口
export const addSquare = (params) => {
  return ajaxRequest(`/square`, "POST", params);
};
export const getSquare = (params) => {
  return ajaxRequest(`/square`, "GET", params);
};

export const deleteSquare = (id) => {
  return ajaxRequest(`/square/${id}`, "DELETE");
};

export const updateSquare = (id, params) => {
  return ajaxRequest(`/square/${id}`, "POST", params);
};


/**
 * blood 相关接口
 */
 export const addBlood = (params) => {
  return ajaxRequest(`/blood/add`, "POST", params);
};
export const getBlood = (params) => {
  return ajaxRequest(`/blood/get`, "POST", params);
};

export const deleteBlood = (id) => {
  return ajaxRequest(`/blood/${id}`, "DELETE");
};

export const updateBlood = (id, params) => {
  return ajaxRequest(`/blood/${id}`, "POST", params);
};

/**
 * 计费管理
 */
export const getPrice = (params) => {
  return ajaxRequest(`/price/get`, "POST", params);
};
export const updatePrice = (id, params) => {
  return ajaxRequest(`/price/${id}`, "POST", params);
};

/**
 * station 相关接口
 */
 export const addStation = (params) => {
  return ajaxRequest(`/station/add`, "POST", params);
};
export const getStation = (params) => {
  return ajaxRequest(`/station/get`, "POST", params);
};

export const deleteStation = (id) => {
  return ajaxRequest(`/station/${id}`, "DELETE");
};

export const updateStation = (id, params) => {
  return ajaxRequest(`/station/${id}`, "POST", params);
};

/**
 * train 相关接口
 */
 export const addTrain = (params) => {
  return ajaxRequest(`/train/add`, "POST", params);
};
export const getTrain = (params) => {
  return ajaxRequest(`/train/get`, "POST", params);
};

export const deleteTrain = (id) => {
  return ajaxRequest(`/train/${id}`, "DELETE");
};

export const updateTrain = (id, params) => {
  return ajaxRequest(`/train/${id}`, "POST", params);
};
/**
 * order 相关接口
 */
 export const addOrder = (params) => {
  return ajaxRequest(`/order/add`, "POST", params);
};
export const getOrder = (params) => {
  return ajaxRequest(`/order/get`, "POST", params);
};

export const deleteOrder = (id) => {
  return ajaxRequest(`/order/${id}`, "DELETE");
};

export const updateOrder = (id, params) => {
  return ajaxRequest(`/order/${id}`, "POST", params);
};
/**
 * passenger 乘坐人 相关接口
 */
 export const addPass = (params) => {
  return ajaxRequest(`/passenger/add`, "POST", params);
};
export const getPass = (params) => {
  return ajaxRequest(`/passenger/get`, "POST", params);
};

export const deletePass = (id) => {
  return ajaxRequest(`/passenger/${id}`, "DELETE");
};

export const updatePass = (id, params) => {
  return ajaxRequest(`/passenger/${id}`, "POST", params);
};

export const test = (id, params) => {
  return ajaxRequest(`/user/abcdefg`, "POST", params);
};

